package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.OverFlowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.OverFlowListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OverFlowListGoodsServiceImpl implements OverFlowListGoodsService {
    @Autowired
    LogService logService;
    @Autowired
    OverFlowListGoodsDao overFlowListGoodsDao;
    @Autowired
    UserDao userDao;
    @Autowired
    GoodsDao goodsDao;


    //新增报溢单
    @Override
    public ServiceVO saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {
        // 使用谷歌Gson将JSON字符串数组转换成具体的集合
        Gson gson = new Gson();

        List<OverflowListGoods> overflowListGoods = gson.fromJson(overflowListGoodsStr,new TypeToken<List<OverflowListGoods>>(){}.getType());

        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());

        overflowList.setUserId(currentUser.getUserId());

        // 保存报溢单信息
        overFlowListGoodsDao.saveOverflowList(overflowList);

        // 保存报溢单商品信息
        for (OverflowListGoods listGoods : overflowListGoods) {
            listGoods.setOverflowListId(overflowList.getOverflowListId());

            overFlowListGoodsDao.saveOverflowListGoods(listGoods);

            // 修改商品库存，状态
//            Goods goods = goodsDao.findByGoodsId(listGoods.getGoodsId());
//
//            goods.setInventoryQuantity(goods.getInventoryQuantity()-listGoods.getGoodsNum());
//
//            goods.setState(2);
//
//            goodsDao.updateGoods(goods);
        }

        // 保存日志
        logService.save(new Log(Log.INSERT_ACTION, "新增报溢单："+overflowList.getOverflowNumber()));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //报溢单查询
    @Override
    public Map<String, Object> getOverflowList(String sTime, String eTime) {

        Map<String,Object> map = new HashMap<>();

        List<OverflowList> list = overFlowListGoodsDao.getOverflowList(sTime,eTime);

        logService.save(new Log(Log.SELECT_ACTION,"查询报溢单信息"));

        map.put("rows", list);

        return map;
    }

    //报溢单商品信息
    @Override
    public Map<String, Object> getOverflowGoodsList(Integer overflowListId) {
        Map<String,Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(overflowListId)){

            List<OverflowListGoods> list = overFlowListGoodsDao.getOverflowGoodsList(overflowListId);
            logService.save(new Log(Log.SELECT_ACTION,"查询报溢单商品信息"));

            map.put("rows", list);
        }


        return map;
    }
}
